/**
 * 
 */
package com.cndym.utils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * @author yihua.shen
 * 2012-06-04
 * 返点日志记录
 */
public class CommissionLog {

	private Log logger;
	private static final String LINK_SIGN = "#";
	private static Map<String, CommissionLog> loggers = new Hashtable<String, CommissionLog>();
	

	private CommissionLog(String type) {
		if ("COMMISSION".equals(type)) {
			this.logger = LogFactory.getLog("com.cndym.commission");
		} else {
			this.logger = LogFactory.getLog(CommissionLog.class);
		}
	}

	public static CommissionLog getInstance(String type) {
		CommissionLog log = loggers.get(type);
		if (log == null) {
			log = new CommissionLog(type);
			loggers.put(type, log);
		}
		return log;
	}

	public void logInfo(String title,String description,String errCode,String errMsg) {
		try {
			StringBuffer sb = new StringBuffer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String systemTime = sdf.format(new Date()).toString();
			sb.append(systemTime).append(LINK_SIGN);
			sb.append(title).append(LINK_SIGN);
			sb.append(description).append(LINK_SIGN);
			sb.append(errCode).append(LINK_SIGN);
			sb.append(errMsg);
			this.logger.info(sb.toString());
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void relationLog(Long userId, String LoginName, Long parentUserId,
			String parentLoginName, Timestamp createTime, Timestamp changeTime,
			Integer relationType, Integer relationLevel, Integer treeLevel,
			Long userAmount, Long groupAmount, BigDecimal betAmount,
			Long commissionAmount, Integer status, String remark) {
		try {
			StringBuffer sb = new StringBuffer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String systemTime = sdf.format(new Date()).toString();
			sb.append(systemTime).append(LINK_SIGN);
			sb.append("RELATION").append(LINK_SIGN);
			sb.append(userId).append(LINK_SIGN);
			sb.append(LoginName).append(LINK_SIGN);
			sb.append(parentUserId).append(LINK_SIGN);
			sb.append(parentLoginName).append(LINK_SIGN);
			sb.append(createTime).append(LINK_SIGN);
			sb.append(changeTime).append(LINK_SIGN);
			sb.append(relationType).append(LINK_SIGN);
			sb.append(relationLevel).append(LINK_SIGN);
			sb.append(treeLevel).append(LINK_SIGN);
			sb.append(userAmount).append(LINK_SIGN);
			sb.append(groupAmount).append(LINK_SIGN);
			sb.append(betAmount.toString()).append(LINK_SIGN);
			sb.append(commissionAmount).append(LINK_SIGN);
			sb.append(remark).append(LINK_SIGN);
			this.logger.info(sb.toString());
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	
	public void recordLog(Long id, Integer logLevel, Integer logType,
			Integer logRank, Long operateUserId, String operateUser,
			Timestamp logTime, String keyWord1, String keyWord2,
			String keyWord3, String keyWord4, String keyWord5, String keyWord6,
			String remark) {
		try {
			StringBuffer sb = new StringBuffer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String systemTime = sdf.format(new Date()).toString();
			sb.append(systemTime).append(LINK_SIGN);
			sb.append("RECORD").append(LINK_SIGN);
			sb.append(id).append(LINK_SIGN);
			sb.append(logLevel).append(LINK_SIGN);
			sb.append(logType).append(LINK_SIGN);
			sb.append(logRank).append(LINK_SIGN);
			sb.append(operateUserId).append(LINK_SIGN);
			sb.append(operateUser).append(LINK_SIGN);
			sb.append(logTime).append(LINK_SIGN);
			sb.append(keyWord1).append(LINK_SIGN);
			sb.append(keyWord2).append(LINK_SIGN);
			sb.append(keyWord3).append(LINK_SIGN);
			sb.append(keyWord4).append(LINK_SIGN);
			sb.append(keyWord5).append(LINK_SIGN);
			sb.append(keyWord6).append(LINK_SIGN);
			sb.append(remark).append(LINK_SIGN);
			this.logger.info(sb.toString());
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void log(String userId, String lotteryId, String playType,
			Float commissionPercent, Timestamp createTime,
			Timestamp changeTime, String remark) {
		try {
			StringBuffer sb = new StringBuffer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String systemTime = sdf.format(new Date()).toString();
			sb.append(systemTime).append(LINK_SIGN);
			sb.append("LOG").append(LINK_SIGN);
			sb.append(userId).append(LINK_SIGN);
			sb.append(lotteryId).append(LINK_SIGN);
			sb.append(playType).append(LINK_SIGN);
			sb.append(commissionPercent).append(LINK_SIGN);
			sb.append(createTime).append(LINK_SIGN);
			sb.append(changeTime).append(LINK_SIGN);
			sb.append(remark).append(LINK_SIGN);
			this.logger.info(sb.toString());
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	public void orderLog(String orderId,String remark) {
		try {
			StringBuffer sb = new StringBuffer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String systemTime = sdf.format(new Date()).toString();
			sb.append(systemTime).append(LINK_SIGN);
			sb.append("ORDEREXT").append(LINK_SIGN);
			sb.append(orderId).append(LINK_SIGN);
			sb.append(remark).append(LINK_SIGN);
			this.logger.info(sb.toString());
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	public void infoLog(String userId, String lotteryId, String playType,
			Float commissionPercent, Timestamp createTime,
			Timestamp changeTime, String remark) {
		try {
			StringBuffer sb = new StringBuffer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String systemTime = sdf.format(new Date()).toString();
			sb.append(systemTime).append(LINK_SIGN);
			sb.append("INFO").append(LINK_SIGN);
			sb.append(userId).append(LINK_SIGN);
			sb.append(lotteryId).append(LINK_SIGN);
			sb.append(playType).append(LINK_SIGN);
			sb.append(commissionPercent).append(LINK_SIGN);
			sb.append(createTime).append(LINK_SIGN);
			sb.append(changeTime).append(LINK_SIGN);
			sb.append(remark).append(LINK_SIGN);
			this.logger.info(sb.toString());
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void errorLog(String operate, String userId,String errCode,String errMsg,String errDescription){
		try {
			StringBuffer sb = new StringBuffer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String systemTime = sdf.format(new Date()).toString();
			sb.append(systemTime).append(LINK_SIGN);
			sb.append("ERROR").append(LINK_SIGN);
			sb.append(operate).append(LINK_SIGN);
			sb.append(userId).append(LINK_SIGN);
			sb.append(errCode).append(LINK_SIGN);
			sb.append(errMsg).append(LINK_SIGN);
			sb.append(errDescription);
			this.logger.info(sb.toString());
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public void operateLog(String operate, String userId) {
		try {
			StringBuffer sb = new StringBuffer();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String systemTime = sdf.format(new Date()).toString();
			sb.append(systemTime).append(LINK_SIGN);
			sb.append("OPERATE").append(LINK_SIGN);
			sb.append(operate).append(LINK_SIGN);
			sb.append(userId);
			this.logger.info(sb.toString());
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
}
